import config from '@/config'
import storage from '@/utils/storage'
import constant from '@/utils/constant'
import { login, logout, findById } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'

const baseUrl = config.baseUrl

const user = {
  state: {
    token: getToken(),
    name: storage.get(constant.name),
    avatar: storage.get(constant.avatar),
	userId: storage.get(constant.userId),
	regionName: storage.get(constant.regionName),
	roleName: storage.get(constant.roleName),
    mobile: storage.get(constant.mobile)
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
      storage.set(constant.name, name)
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
      storage.set(constant.avatar, avatar)
    },
	SET_USERID: (state, userId) => {
      state.userId = userId
      storage.set(constant.userId, userId) 
    },
	SET_REGIONNAME: (state, regionName) => {
	  state.regionName = regionName
	  storage.set(constant.regionName, regionName) 
	},
	SET_ROLENAME: (state, roleName) => {
	  state.roleName = roleName
	  storage.set(constant.roleName, roleName) 
	},
	SET_MOBILE: (state, mobile) => {
	  state.mobile = mobile
	  storage.set(constant.mobile, mobile) 
	}
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const mobile = userInfo.mobile
      const code = userInfo.code
      
      return new Promise((resolve, reject) => { 
        login(mobile, code).then(res => {
			if(res.success){
				setToken(res.token)
				commit('SET_TOKEN', res.token)
				commit('SET_USERID', res.userId)
				 // localStorage.setItem('userId', String(userId));
				resolve(res)
			}else{
			// 确保 res 是字符串类型，并截取合适长度以适应 showToast 的显示
			 const message = typeof res === 'string' ? res.slice(0, 30) : '未知消息'; // 根据需要调整截取长度
			 
			 uni.showToast({
			   title: message,
			   icon: 'error'
			 });
			}
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetFindById({ commit, state }) {
      return new Promise((resolve, reject) => {
		const userId = state.userId;
        findById(userId).then(res => {
          const avatar = (res.image == "" || res.image == null) ? require("@/static/images/profile.jpg") : res.image
		  commit('SET_AVATAR', avatar)
		  commit('SET_NAME', res.userName)
		  commit('SET_REGIONNAME', res.regionName)
		  commit('SET_ROLENAME', res.roleName)
		  commit('SET_MOBILE', res.mobile)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          removeToken()
          storage.clean()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

export default user
